//
//  PeopleCenterTableViewController.m
//  cityStar
//
//  Created by apple on 14/12/14.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "PeopleCenterTableViewController.h"
#import "HTDefine.h"
#import "SideViewController.h"
#import "LoginCityStarViewViewController.h"
#import "BaseNavController.h"
#import "ModifySecurityViewController.h"
#import "MyOrdersViewController.h"

#import "ShopSaveViewController.h"
#import "GoodsSaveViewController.h"
#import "PersonalDetail.h"
#import "UIImageView+WebCache.h"
#import "MyCommunityViewController.h"
#import "HWSearchViewController.h"
#import "MyPublishViewController.h"

@interface PeopleCenterTableViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imgHead;
@property (weak, nonatomic) IBOutlet UILabel *labelName;
@property (weak, nonatomic) IBOutlet UILabel *labelLevel;
@property (weak, nonatomic) IBOutlet UILabel *labelScore;
@property (weak, nonatomic) IBOutlet UILabel *labelMoney;
@property (weak, nonatomic) IBOutlet UILabel *labelOrder;
@property (weak, nonatomic) IBOutlet UILabel *labelCommunty;
@property (weak, nonatomic) IBOutlet UILabel *labelGoodsCollect;
@property (weak, nonatomic) IBOutlet UILabel *labelsupplierCollect;
@property (weak, nonatomic) IBOutlet UILabel *labelActive;
@property (weak, nonatomic) IBOutlet UILabel *labelInvest;
@property (weak, nonatomic) IBOutlet UILabel *labelPublish;

@end

@implementation PeopleCenterTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavBar];
    [self initViews];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 55, 0, 0);
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginSuccessRefreshPeopleCenter) name:@"LoginCityStarViewSuccess" object:nil];
//    [self presentLoginViewController];
}


-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = NO; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = kWhiteNavTitleColor; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor]; //bar背景颜色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }else{
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }
        [self loginSuccessRefreshPeopleCenter];
}

//-(void)viewDidDisappear:(BOOL)animated
//{
//    self.isLoginFirstTime = NO;
//}

- (void)initNavBar{
    self.navigationItem.title = @"个人中心";
    
    UIButton * regBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    regBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [regBtn setTitleColor:rgba(133, 133, 133, 1) forState:UIControlStateHighlighted];
    [regBtn setImage:[UIImage imageNamed:@"bsearch"] forState:UIControlStateNormal];
    [regBtn setImage:[UIImage imageNamed:@"bsearch_touch"] forState:UIControlStateHighlighted];
    [regBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * iHaveNewsBtnItem = [[UIBarButtonItem alloc]initWithCustomView:regBtn];
    self.navigationItem.rightBarButtonItem = iHaveNewsBtnItem;
    
    if (self.isPush == NO) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
        [leftButton setTitle:@"返回" forState:UIControlStateNormal];
        leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
        [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
        [leftButton addTarget:self action:@selector(sideMove) forControlEvents:UIControlEventTouchUpInside];
        [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
        
        [view addSubview:leftButton];
        
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
        self.navigationItem.leftBarButtonItem = barButtonItem;
//        UIButton *btnSide = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
//        
//        [btnSide setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
//        [btnSide setImage:[UIImage imageNamed:@"more_touch"] forState:UIControlStateHighlighted];
//        [btnSide addTarget:self action:@selector(sideMove) forControlEvents:UIControlEventTouchUpInside];
//        UIBarButtonItem *bbtmLeft = [[UIBarButtonItem alloc]initWithCustomView:btnSide];
//        self.navigationItem.leftBarButtonItem = bbtmLeft;
    }else{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
        [leftButton setTitle:@"返回" forState:UIControlStateNormal];
        leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
        [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
        [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
        
        [view addSubview:leftButton];
        
        UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
        self.navigationItem.leftBarButtonItem = barButtonItem;
    }
}

- (void)searchAction{
    HWSearchViewController *hwSearchVC = [[HWSearchViewController alloc]init];
    hwSearchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:hwSearchVC animated:YES];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sideMove{
    [[SideViewController shareVc] showLeftView];
}
- (void)initViews{
    self.tableView.backgroundColor = kBackgroundColor;
    
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 0;
    
    self.imgHead.clipsToBounds = YES;
    self.imgHead.layer.cornerRadius = 5;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark- UITableViewDataSource Methods
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return 15;
    }else if (section == 1){
        return 10;
    }else if (section == 2){
        return 10;
    }else if (section == 3){
        return 10;
    }
    return 0;
}



- (IBAction)ToOrdinary_Action:(id)sender {
    ModifySecurityViewController *modifySecurityViewController = [[ModifySecurityViewController alloc] init];
    [self.navigationController pushViewController:modifySecurityViewController animated:true];
    
}
- (IBAction)jumpToShoppingList_Touch:(id)sender {
    DLog(@"进入个人订单");
    MyOrdersViewController *myOrderVC = [[MyOrdersViewController alloc]init];
    [self.navigationController pushViewController:myOrderVC animated:YES];
    
}

- (IBAction)goodsSave_Action:(id)sender {
    GoodsSaveViewController *goodSaveVC = [[GoodsSaveViewController alloc]init];
    [self.navigationController pushViewController:goodSaveVC animated:YES];
}

- (IBAction)shopSave_Action:(id)sender {
    ShopSaveViewController *shopSaveVC = [[ShopSaveViewController alloc]init];
    [self.navigationController pushViewController:shopSaveVC animated:YES];
}
- (IBAction)myPublish:(id)sender {
    MyPublishViewController *myPublishMainVC = [[MyPublishViewController alloc]init];
    [self.navigationController pushViewController:myPublishMainVC animated:YES];
}

#pragma mark - IsJumpToLoginViewController
-(void)presentLoginViewController
{
    LoginCityStarViewViewController *loginCityStarVC = [[LoginCityStarViewViewController alloc]init];
    BaseNavController *baseloginNav = [[BaseNavController alloc] initWithRootViewController:loginCityStarVC];
    [self presentViewController:baseloginNav animated:YES completion:^{
    }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView reloadData];
    if (indexPath.section == 2) {
        if (indexPath.row == 1 || indexPath.row == 0) {
            BossSystemShowAlertViewWithTitle(@"正在建设中，敬请期待");
        }else if (indexPath.row == 3)
        {
            MyCommunityViewController *myCommunityVC = [[MyCommunityViewController alloc]init];
            [self.navigationController pushViewController:myCommunityVC animated:YES];
        }
    }
}

#pragma mark - WebRequest
-(void)loginSuccessRefreshPeopleCenter
{
    [HttpRequestManager getMethod:KInterFace_getPersonalDetail parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            
            PersonalDetail *personalDetail = [PersonalDetail objectFromDictionary:response[@"value"]];
//            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:personalDetail];
//            [KStandUserDefaults setObject:data forKey:KUserDefaultKey_PersonalDetail];
//            [KStandUserDefaults synchronize];
            
            [self.imgHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/160/h/155/m/0",personalDetail.avatar]] placeholderImage:[UIImage imageNamed:@"头像@"]];
            self.labelName.text = personalDetail.nickname;
            self.labelLevel.text = [personalDetail.gender boolValue]?@"普通会员":@"白金会员";
            self.labelScore.text = [NSString stringWithFormat:@"用户积分:%@",personalDetail.score];
            self.labelMoney.text = [NSString stringWithFormat:@"%.2f",[personalDetail.money floatValue]];
            self.labelOrder.text = [NSString stringWithFormat:@"%@",personalDetail.order_number];
            self.labelsupplierCollect.text = [NSString stringWithFormat:@"%@",personalDetail.supplier_collection_number];
            self.labelCommunty.text = [NSString stringWithFormat:@"%@",personalDetail.community];
            self.labelGoodsCollect.text = [NSString stringWithFormat:@"%@",personalDetail.goods_collection_number];
            self.labelActive.text = [NSString stringWithFormat:@"%@",personalDetail.activity_number];
            self.labelInvest.text = [NSString stringWithFormat:@"%@",personalDetail.invest_numebr];
            self.labelPublish.text = [NSString stringWithFormat:@"%@",personalDetail.publish];
            self.labelMoney.text = [NSString stringWithFormat:@"%@",personalDetail.money];

        }else{
            if ([errMsg isEqualToString:@"请先登入"] == YES) {
//                [self presentLoginViewController];
            }
        }
    }];
}

/*
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
 
 // Configure the cell...
 
 return cell;
 }
 */

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    PublishMainTableViewController *tvcPeopleCenter = segue.destinationViewController;

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}


@end
